package org.dragonfire.editor.z80;

import java.net.URL;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
import org.osgi.framework.Bundle;

public class AsmEditor extends TextEditor {

	public static final String PLUGIN_ID = "org.dragonfire.editor";
	private Bundle bundle = Platform.getBundle(PLUGIN_ID);
	private ImageRegistry imageRegistry = new ImageRegistry();

	public ImageRegistry getImageRegistry() {
		return imageRegistry;
	}

	public void registerImage(String filePath, String name) {
		IPath path = new Path(filePath);
		URL url = Platform.find(bundle, path);
		ImageDescriptor desc = ImageDescriptor.createFromURL(url);
		getImageRegistry().put(name, desc);
	}

	private ColorManager colorManager;
	private ContentOutlinePage outline;

	public AsmEditor() {
		super();
		colorManager = new ColorManager();
		setSourceViewerConfiguration(new AssemblyConfiguration(colorManager));
		setDocumentProvider(new XMLDocumentProvider());
		registerImage("icons/sample.gif", "sample");
	}

	public void dispose() {
		colorManager.dispose();
		super.dispose();
	}

	public Object getAdapter(Class key) {
		if (key.equals(IContentOutlinePage.class)) {
			return showOutlineView() ? getContentOutlinePage() : null;
		}
		/*
		 * else if (key.equals(IPropertySheetPage.class)) { return
		 * getPropertySheetPage(); } else if (key.equals(IGotoMarker.class)) {
		 * return this; }
		 */
		else {
			return super.getAdapter(key);
		}
	}

	public boolean showOutlineView() {
		return true;
	}

	public IContentOutlinePage getContentOutlinePage() {
		if (outline == null)
			outline = new AsmOutlinePane(this);
		return outline;
	}

	public IDocument getDocument() {
		return getSourceViewer().getDocument();
	}

	public ISourceViewer getSource() {
		return super.getSourceViewer();
	}

}
